<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :xs="24" :sm="24" :lg="7">
        <el-card class="box-card" :style="'height:' + height">
          <myArea :device_this="sup_this"/>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="17">
        <el-card class="box-card" :style="'height:' + height">
          <eHeader :query="query" :dict="dict" :sup_this="sup_this"/>
          <!--表格渲染-->
          <el-table v-loading="loading" :data="data" size="small" style="width: 100%;">
            <el-table-column fixed prop="deviceName" label="设备名称" min-width="120"/>
            <el-table-column prop="deviceIp" label="设备IP"  min-width="120"/>
            <el-table-column prop="devicePort" label="设备端口"/>
            <el-table-column prop="username" label="用户名"/>
            <el-table-column prop="password" label="密码"/>
            <el-table-column label="设备区域">
              <template slot-scope="scope">
                <div v-for="item in dict.area" :key="item.id">
                  <span v-if="scope.row.deviceArea==item.id">{{ item.areaName }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="inOutFlag" label="进出标识">
              <template slot-scope="scope">
                <div v-for="item in dict.in_out_flag" :key="item.id">
                  <span v-if="scope.row.inOutFlag==item.value">{{ item.label }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="attendance" label="是否考勤">
              <template slot-scope="scope">
                <div v-for="item in dict.is_no" :key="item.id">
                  <span v-if="scope.row.attendance==item.value">{{ item.label }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="deviceModel" label="设备型号"/>
            <el-table-column prop="deviceNum" label="设备编号"/>
            <el-table-column prop="deviceGrade" label="设备级别"/>
            <el-table-column prop="deviceBrand" label="设备品牌"/>
            <el-table-column prop="manufacturer" label="设备厂商"/>
            <el-table-column prop="deviceType" label="设备类型"/>
            <el-table-column fixed="right" label="操作" width="150px" align="center">
              <template slot-scope="scope">
                <edit :dict="dict" :data="scope.row" :sup_this="sup_this"/>
                <el-popover
                  :ref="scope.row.id"
                  placement="top"
                  width="180">
                  <p>确定删除本条数据吗？</p>
                  <div style="text-align: right; margin: 0">
                    <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
                    <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定
                    </el-button>
                  </div>
                  <el-button slot="reference" type="danger" size="mini">删除</el-button>
                </el-popover>
              </template>
            </el-table-column>
          </el-table>
          <!--分页组件-->
          <el-pagination
            :total="total"
            style="margin-top: 8px;"
            layout="total, prev, pager, next, sizes"
            @size-change="sizeChange"
            @current-change="pageChange"/>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import checkPermission from '@/utils/permission'
  import initData from '@/mixins/initData'
  import {del, getDict} from '@/api/accessDevice'
  import {parseTime} from '@/utils/index'
  import eHeader from './module/header'
  import edit from './module/edit'
  import myArea from '@/views/system/area'
  export default {
    components: {eHeader, edit, myArea},
    mixins: [initData],
    data() {
      return {
        height: document.documentElement.clientHeight - 104 + 'px;',
        delLoading: false, sup_this: this,
        dict: {}
      }
    },
    created() {
      this.$nextTick(() => {
        this.init()
        this.getDict()
      })
    },
    methods: {
      parseTime,
      checkPermission,
      getDict(){
        getDict().then(res => {
          this.dict = res
        }).catch(err => {
          console.log(err.response.data.message)
        })
      },
      beforeInit() {
        this.url = 'api/accessDevice'
        const sort = 'id,desc'
        this.params = {page: this.page, size: this.size, sort: sort}
        const query = this.query
        const deviceName = query.deviceName
        const deviceArea = query.deviceArea
        if (deviceName) {
          this.params['deviceName'] = deviceName
        }
        if (deviceArea) {
          this.params['deviceArea'] = deviceArea
        }
        return true
      },
      subDelete(id) {
        this.delLoading = true
        del(id).then(res => {
          this.delLoading = false
          this.$refs[id].doClose()
          this.init()
          this.$notify({
            title: '删除成功',
            type: 'success',
            duration: 2500
          })
        }).catch(err => {
          this.delLoading = false
          this.$refs[id].doClose()
          console.log(err.response.data.message)
        })
      }
    }
  }
</script>

<style scoped>

</style>
